using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WTCOTH
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public partial class Game1 : Microsoft.Xna.Framework.Game
    {
        //private string gameState = "splash";
        enum GameState
        {
            Splash,
            StartMenu,
            LoadGame,
            LoadingSplash,
            CreateCharacter,
            NameCharacter,
            Playing,
            Paused,
            Options
        }
        main_menu menu;
        GameState gameState = new GameState();
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D brick, dirt, grass, sand, stone, water, water2, SplashScreen,buttonBg;
        SpriteFont font;
        int mapHeight, mapWidth, screenWidth, screenHeight;
        map Map = new map();
        button b;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            this.graphics.IsFullScreen = true;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            screenHeight = graphics.GraphicsDevice.Viewport.Height;
            screenWidth = graphics.GraphicsDevice.Viewport.Width;
            base.Initialize();
            gameState = GameState.Splash;
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            brick = this.Content.Load<Texture2D>("texture/brick");
            dirt = this.Content.Load<Texture2D>("texture/dirt");
            grass = this.Content.Load<Texture2D>("texture/grass");
            sand = this.Content.Load<Texture2D>("texture/sand");
            stone = this.Content.Load<Texture2D>("texture/stone");
            water = this.Content.Load<Texture2D>("texture/water");
            water2 = this.Content.Load<Texture2D>("texture/water2");
            font = this.Content.Load<SpriteFont>("test");
            SplashScreen = this.Content.Load<Texture2D>("texture/splash");
            buttonBg = this.Content.Load<Texture2D>("texture/button");

            b = new button(spriteBatch, buttonBg, Color.White, 466, 73, 167, 50);
            if (Map.load_map(this.Content.Load<Texture2D>("map/1")))
            {
                mapHeight = Map.get_map_height("pixel");
                mapWidth = Map.get_map_width("pixel");
            }
            menu = new main_menu(spriteBatch, font);

        }
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);

            if (b.is_pressed())
            {
                //gameState = GameState.Playing;
            }
        }
        int frame, f2 = 1;
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            /*
             *
             *  TODO:
             *  Clean up this function, and extract extra bits to other functions
             * 
             * */
            spriteBatch.Begin(new SpriteSortMode(), BlendState.AlphaBlend);
            GraphicsDevice.Clear(Color.Black);

            switch (gameState)
            {
                case GameState.Splash:
                    drawsplash("Loading");
                    break;
                case GameState.StartMenu:
                    button newGame = new button(spriteBatch, buttonBg, Color.White, 400, 50, 200, 50);
                    button loadGame = new button(spriteBatch, buttonBg, Color.White, 400, 50, 200, 150);
                    button options = new button(spriteBatch, buttonBg, Color.White, 400, 50, 200, 250);
                    button quit = new button(spriteBatch, buttonBg, Color.White, 400, 50, 200, 350);
                    newGame.draw();
                    loadGame.draw();
                    options.draw();

                    quit.draw();
                // start up menu (load game, new game, etc)
                    break;
                case GameState.CreateCharacter:
                    // create character
                    break;
                case GameState.LoadGame:
                    // load game
                    break;
                case GameState.LoadingSplash:
                    // 
                    break;
                case GameState.Playing:
                    DrawWorld();
                    DrawPlayer();
                    break;
                case GameState.Paused:
                   
                    b.draw();
                    break;
            }



            spriteBatch.DrawString(font, "Watermark~! ^___^", new Vector2(400, 200), new Color(128,0,0,100));
            spriteBatch.End();

            base.Draw(gameTime);

            // Hacky animation stuff ^__^
            frame++;

            if (frame % 5 == 0)
            {
                f2 *= -1;
            }
            if (frame == 100)
            {
                gameState = GameState.StartMenu;
            }
            if (frame == 200)
            {
                frame = 100;
            }
        }
    }
}
